<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

/**
 * App\Models\SettingChannel
 *
 * @property int $id
 * @property string $channel_code 资方标号
 * @property string $channel_name 资方名称
 * @property string $period_range 借款的期限范围
 * @property int $is_show_plan_table 是否显示还款计划表
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingChannel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingChannel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingChannel query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingChannel whereChannelCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingChannel whereChannelName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingChannel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingChannel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingChannel whereIsShowPlanTable($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingChannel wherePeriodRange($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingChannel whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class SettingChannel extends Model
{

    static $CHANNEL_LIST = [
        self::CHANNEL_GUANGDA => '光大银行',
        self::CHANNEL_JIXIAO => '晋消',
        self::CHANNEL_OLD_JINSHANG => '晋商+新光大',
        self::CHANNEL_OLD_GUANGDA => '老-老光大',
        self::CHANNEL_OLD_ZHONGAN => '老-众安',
        self::CHANNEL_OLD_LONGXIN => '老-龙信',
        self::CHANNEL_OLD_LONGXIN_WEISHANG => '老-龙信微商',
        self::CHANNEL_OLD_WEIYAN_TWO => '老-微言2',
        self::CHANNEL_OLD_WEIYAN => '老-微言',
    ];

    const CHANNEL_GUANGDA = '10009';
    const CHANNEL_JIXIAO = 'JCFC';
    const CHANNEL_NONGXIN = 0;
    const CHANNEL_OLD_JINSHANG = 'LCC201709190010'; //老数据  晋商+新光大
    const CHANNEL_OLD_GUANGDA = 'LCC201709190009';//老数据  老光大
    const CHANNEL_OLD_ZHONGAN = 'LCC201709190002';//老数据  众安
    const CHANNEL_OLD_LONGXIN = 'LCC201709190006';//老数据  龙信
    const CHANNEL_OLD_LONGXIN_WEISHANG = 'LCC201709190007';//老数据  龙信微商
    const CHANNEL_OLD_WEIYAN_TWO = 'LCC201709190011';//老数据  微言2
    const CHANNEL_OLD_WEIYAN = 'LCC201709190008';//老数据  微言


    //是否显示还款计划表
    const IS_SHOW_PLAN_ZERO = 0; //不显示
    const IS_SHOW_PLAN_ONE = 1; //显示

    //
    protected $guarded = ['id'];

    /**
     * Description:判断是否存在channel_code对应资方
     * Author:lijiafei
     * @param $channel_code
     * @return Model|null|object|static
     */
    public static function hasChannelCode($channel_code){
        return self::where('channel_code',$channel_code)->first();
    }

    /**
     * Description:获取资方
     * Author:lijiafei
     * @param $channel 资方编号(渠道号)
     */
    public static function getChannelNameByChannel($channel = ''){
        return self::where('channel_code',$channel)->value('channel_name');
    }

    /**
     * 更改和这个channel_code有关联的其他设置信息
     */
    public static function updateChannelCode($id,$channel_code){
        $updateData['channel_code'] = $channel_code;
        $oldChannelInfo = self::where('id',$id)->select('channel_code')->first();
        $oldChannelCode = $oldChannelInfo['channel_code'];
        DB::beginTransaction();
        try{
            //更新documents表
            SettingDocument::where('channel_code',$oldChannelCode)->update($updateData);
            //更新提示语表
            SettingHint::where('channel_code',$oldChannelCode)->update($updateData);
            //更新还款利率表
            SettingRepayRate::where('channel_code',$oldChannelCode)->update($updateData);
            DB::commit();
            return true;
        }catch (\Exception $e){
            DB::rollBack();
            Log::debug('资方设置:' . $e->getMessage());
            return false;
        }
    }

    /**
     * Description:根据渠道号来判断是否要在前端显示还款计划表
     * Author:lijiafei
     * @param string $channel_code 渠道号
     * @return int|mixed 0 不显示计划表 1 显示计划表
     */
    public static function isShowPlan($channel_code){
        $is_show_plan_table =  self::where('channel_code',$channel_code)->value('is_show_plan_table');
        if(!$is_show_plan_table){
            return 0;
        }
        return $is_show_plan_table;
    }
}
